/*!
 * \file Singleton.h
 * \brief Singleton template.
 * \author Nus
 * \date 2007/02/12, 18:16
 */

#ifndef __SINGLETON_H__
#define __SINGLETON_H__

#include "Debug.h"
#include "Allocator.h"

//! \brief Define a singleton.
#define DEFINE_SINGLETON(_class) \
private: \
friend class Singleton< _class >

//! \brief Get instance.
#define INST(_class) _class::getInstance()

/*!
 * \class Singleton
 * \brief Singleton template.
 */
template< class T >
class Singleton
{
public:
  //! \brief Initialize singleton object.
  static T* initialize(void) {
    if(!mpInstance)
      mpInstance = nuNew T;
    return mpInstance;
  }
  
  //! \brief Terminate singleton object.
  static void terminate(void) {
    SAFE_DELETE(mpInstance);
  }

  //! Get singleton object's instance pointer.
  static T* getInstance(void) {
    return mpInstance;
  }
  
protected:
  static T* mpInstance;           //!< Pointer to singleton instance.
  
  //! Default constructor.
  Singleton() {
    // If any...
  }
  
  //! Default destructor.
  virtual ~Singleton() {
    // If any...
  }

};

//! Singleton instance.
template< class T >
T* Singleton< T >::mpInstance = NULL;

#endif
